<template>
	<gui-page ref="guiPage" :customHeader="true">
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :title="$t('multilingual.print')"  @clickLeftBack="clickLeftBack"></nav-bar-custom>
		</template>
		<template v-slot:gBody>
			<view class="distributed">
				<scroll-view :show-scrollbar="false" :scroll-y="true" class="scroll" @scrolltolower="scrolltolowerFn">
					<view class="record-card" v-for="(item, index) in data.list" :key="`${item.barcode}` + `${index}`">
						<view class="card-mini-head">
							<view class="title-line"></view>
							<text>{{ $t('translationImage.serialNumber') }}：{{ data.list.length - index }}</text>
						</view>
						<view  class="custom-card-itemOne split" v-if="item.parentBarcode!=null" style="font-weight: 600;margin:10rpx 0">
							<text class="item-info-title" style="font-weight: 600;color:#333">{{ $t('warehousing.fatherBarcode') }}：</text>{{ item.parentBarcode }}
						</view>
						<view class="custom-card-itemOne">
							<text class="item-info-title">{{ $t('distributedOut.barcode') }}：</text>{{ item.barcode }}
						</view>
						<view class="custom-card-itemOne">
							<text class="item-info-title">{{ $t('distributedEnter.materialCode')}}：</text>{{ item.materialCode }}
						</view>
						<view class="custom-card-itemOne">
							<text class="item-info-title">{{ $t('distributedEnter.materialName')}}：</text>{{ item.materialName }}
						</view>
						<view class="custom-card-itemOne">
							<text class="item-info-title">{{ $t('distributedEnter.materialSpec')}}：</text>{{ item.materialSpec || '' }}
						</view>
						<view class="custom-card-itemOne">
							<view class="card-item-50">
								<text class="item-info-title">{{ $t('distributedEnter.uom') }}：</text>{{ item.unit }}
							</view>
							<view class="card-item-50">
								<text class="item-info-title">{{ $t('distributedEnter.qty') }}：</text>{{ item.currentQty }}
							</view>
						</view>
						<view class="custom-card-itemOne">
							<view class="card-item-50">
								<text class="item-info-title">{{
									$t('translationImage.transferredWarehouse') }}：</text>{{ item.warehouseCode }}
							</view>
							<view class="card-item-50">
								<text class="item-info-title">{{ $t('translationImage.transferredSeat') }}：</text>{{
									item.locationCode }}
							</view>
						</view>
						<view class="custom-card-itemOne">
							<text class="item-info-title">{{ $t('translationImage.process') }}：</text>{{ item.processCode }}
						</view>
						<view class="custom-card-itemOne gui-flex gui-justify-content-end">
							<view class="print-class" @click="handelPrint(item)">
								{{ $t('multilingual.printBarcode') }}
							</view>
						</view>
					</view>
				</scroll-view>
				<!-- 底部确定按钮 -->
				<view class="page-bottom-box bottom-postion">
					<view class="page-bottomBtn bottomBtn-bgcolor-green" @click="handelPrintAll">{{
						$t('warehousing.printAll') }}</view>
				</view>
			</view>
		</template>
	</gui-page>
</template>

<script setup>
import { navigate } from '@/utils/routerSkip.js';
import { BIGBARCODER_PRINT } from '@/constant/index.js'
import { useStore } from "vuex";
import { ref, reactive, onMounted, watch, computed, toRaw } from 'vue';
import { basPdaOperationLogFn } from '@/utils/index.js';
import { OPERATION_DEL, OPERATION_LOOK } from '@/constant/index.js'
import { getMaterialTransferBarcode, printsLabels1 } from '@/api/api.js'

//JS国际化引入
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

let data = reactive({
	barcodeList: [],//条码数组
	showPull: true,
	list: [],
	defaultValue: '', //打印机名字
})

let store = useStore();

data.barcodeList = computed(() => {
	return store.state.allotListSplit
})

onMounted(() => {
	data.defaultValue = uni.getStorageSync(BIGBARCODER_PRINT) || '';
	if (data.defaultValue == '') {
		uni.showModal({
			content: t('JsInternational.plsPrinter'),
			showCancel: false,
			confirmText: t('pageTranslation.toSet'),
			success: function (res) {
				if (res.confirm) {
					uni.navigateTo({
						url: '/pages/setting/printer'
					})
				} else if (res.cancel) {

				}
			}
		});
	}
	try {
		let inputParameter = '无'
		let returnParameter = JSON.stringify(data.barcodeList)
		let operationPicture = '原材料调拨打印'
		let callInterface = '无'
		let opertionParmes = {
			inputParameter,
			returnParameter,
			operationPicture,
			callInterface,
			responseCode: '无',//返回状态
			errorMessage: '无',//错误信息
			isPass: 'Y',//是否成功 Y N
			operationType: OPERATION_LOOK
		}
		basPdaOperationLogFn(opertionParmes)
	} catch (error) {
	}
	getMaterialTransferBarcode(data.barcodeList).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '原材料调拨打印'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.status,//返回状态
				errorMessage: res.message,//错误信息
				isPass: res.status == 200 ? 'Y' : 'N',//是否成功 Y N
				operationType: OPERATION_COMMIT
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.status == 200) {
			data.list = res.data
		} else {
			uni.showToast({ title: res.message, icon: "none" });
			return
		}
		console.log(res)
	})
})

// 单个打印
const handelPrint = (item) => {
	console.log(item)
	if (item == null) {
		uni.showToast({ title: t('stockManage.pleaseScanCode'), icon: "none" });
		return
	}
	let parmase = {
		barCode: item.barcode,
		materialCode: item.materialCode,
		materialName: item.materialName,
		specification: item.materialSpec,
		LocationCode: item.locationCode,
		produceDate: item.produceDate ? item.produceDate.slice(0, 10) : '', // 生产日期
		hostid: item.equipmentCode || '', //机台
		qty: item.currentQty,
		unm: item.unit, //单位
		printerName: data.defaultValue, //打印机名字
		splitStatus: item.parentBarcode!=null?'拆':''
	}
	// console.log(parmase)
	uni.showLoading({
		title: t('materials.connecting')
	});
	printsLabels1(parmase).then(res => {
		uni.hideLoading();
		try {
			let inputParameter = JSON.stringify(parmase)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '原材料调拨打印'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.status,//返回状态
				errorMessage: res.message,//错误信息
				isPass: res.status == 200 ? 'Y' : 'N',//是否成功 Y N
				operationType: OPERATION_PRINT
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.status == 200) {
			uni.showToast({ title: t('JsInternational.printSuccessfully'), icon: "none" });
		} else {
			uni.showToast({ title: res.message, icon: "none" });
		}
	}).catch(() => {
		try {
			let inputParameter = JSON.stringify(parmase)
			let returnParameter = '无'
			let operationPicture = '原料调拨打印'
			let callInterface = printUrl + "/print/RawMaterialBarCodePrint"
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: 408,//返回状态
				errorMessage: '请求超时',//错误信息
				isPass: 'N',//是否成功 Y N
				operationType: OPERATION_PRINT
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		uni.showToast({ title: t('materials.printingFailed'), icon: "none" });
		uni.hideLoading();
	})
}

// 全部打印
const handelPrintAll = () => {
	let arr = data.list
	uni.showLoading({
		title: t('materials.connecting')
	});
	function testPromise(item) {
		return new Promise((resolve, reject) => {
			// console.log(item)
			let parmase = {
				barCode: item.barcode,
				materialCode: item.materialCode,
				materialName: item.materialName,
				specification: item.materialSpec,
				LocationCode: item.locationCode,
				produceDate: item.produceDate ? item.produceDate.slice(0, 10) : '', // 生产日期
				hostid: item.equipmentCode || '', //机台
				qty: item.currentQty,
				unit: item.unit, //单位
				printerName: data.defaultValue, //打印机名字
				splitStatus: item.parentBarcode!=null?'拆':''
			}
			console.log(parmase)
			printsLabels1(parmase).then(res => {
				try {
					let inputParameter = JSON.stringify(parmase)
					let returnParameter = JSON.stringify(res)
					let operationPicture = '原材料调拨打印'
					let callInterface = printUrl + '/print/barCodePrint'
					let opertionParmes = {
						inputParameter,
						returnParameter,
						operationPicture,
						callInterface,
						responseCode: res.status,//返回状态
						errorMessage: res.message,//错误信息
						isPass: res.status == 200 ? 'Y' : 'N',//是否成功 Y N
						operationType: OPERATION_SURE
					}
					basPdaOperationLogFn(opertionParmes)
				} catch (error) {
				}
				console.log(res, 'reessss')
				uni.hideLoading();
				if (res.status == 200) {
					//打印成功之后调用保存
					uni.showToast({ title: t('JsInternational.printSuccessfully'), icon: "none" });
					resolve(item);
				} else {
					data.printSuccessFlag = false;
					uni.showToast({ title: res.message, icon: "none" });
					reject()
				}
			})
		});
	}
	let result = arr.reduce((accumulatorPromise, item) => {
		return accumulatorPromise.then(() => {
			return testPromise(item);
		});
	}, Promise.resolve());
	result.then(e => {
		uni.hideLoading();
	});
}

//导航栏左侧返回
const clickLeftBack = () => {
	if (!data.list.length) {
		uni.navigateBack({
			delta: 1
		});
		return
	}
	uni.showModal({
		title: t('smashRemoval.warmPrompt'),
		content: t('materials.clearedIfReturned'),
		success: function (res) {
			if (res.confirm) {
				store.commit('reactAllotListSplit')
				uni.navigateBack({
					delta: 1
				});
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}


const scrolltolowerFn = () => {
	if (!data.noticeHooks) {
		data.page++
	}
}
</script>

<style scoped lang="scss">
.distributed {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 88rpx);
	/* // height: 100vh; */
}

.header {
	height: 60rpx;
	font-size: 24rpx;
	width: 100%;
	line-height: 60rpx;
	padding: 0 40rpx 0 20rpx;
	box-sizing: border-box;
	background-color: #fff;
	border-bottom: 2rpx dashed #cecece;
	position: relative;
	color: #6B6969;
}

.queryMessage_list {
	height: 120rpx;
	width: 100%;
	overflow: hidden;

	&_card {
		color: rgb(107, 105, 105);
		letter-spacing: 4rpx;
		margin: 20prx;
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		border-bottom: 2rpx dashed #cecece;
	}

	.queryMessage_list_div {
		width: 100%;
		height: 60rpx;
		display: flex;
		flex-direction: row;
	}

	&_frist {
		height: 60rpx;
		width: 50%;
		background-color: #fff;
		display: flex;
		flex-direction: row;
	}

	&_title {
		height: 60rpx;
		width: auto;
		font-size: 26rpx;
		text-align: center;
		line-height: 60rpx;
		letter-spacing: 2rpx;
		padding: 0 20rpx 0 20rpx;
		box-sizing: border-box;
	}

	&_spec {
		flex: 1 1 auto;
		font-size: 20rpx;
		text-align: left;
		line-height: 60rpx;
	}

	&_tow {
		width: 50%;
		background-color: #fff;
		display: flex;
		flex-direction: row;
	}

	&_three {
		width: 100%;
		display: block;
		background-color: #fff;
		display: flex;
		flex-direction: row;
	}
}

.scroll {
	flex: 1;
	height: 2rpx;
	margin: 0 0 120rpx 0;
}
.page-bottom-box {
	position: absolute;
	left: 0px;
	bottom: 0px;
	width: 100%;
}

.print-class {
	display: inline-block;
	padding: 10rpx 20rpx;
	background-color: chartreuse;
	color: #fff;
	border-radius: 8rpx;
	background: $uni-color-teal;
}

.split{
	position: relative;
}

.split::after{
	position: absolute;
	right: 0;
	top: -50rpx;
	content:'拆(split)';
	font-weight: 100;
	font-size: 12px;
	height: 40rpx;
	line-height: 40rpx;
	width: 100rpx;
	background-color:#fc3a3a;
	color: #fff;
	border-radius: 8rpx;
	text-align: center;
}
</style>